Back to Main Menu

Uploading Documents

Assetic Document Upload Sample

The following demonstrates how a python script may be used to upload documents unto the Assetic online repository.

  1. """
  2. Example script to upload a document(Assetic.DocumentUpload.py)
  3. This script uploads a file to assetic. Can link the file to a record
  4. such as an asset or work order
  5. """
  6. import assetic
  7. import os
  8. import datetime
  9. import base64
  10. import sys
  11. ##Assetic SDK instance
  12. asseticsdk = assetic.AsseticSDK(None,None,'Info')
  13. docapi = assetic.DocumentApi()
  14. fullfilename = "C:/temp/Assetic.txt"
  15. filename = os.path.basename(fullfilename)
  16. fullpathnoext,fileext = os.path.splitext(fullfilename)
  17. with open(fullfilename, "rb") as f:
  18. data = f.read()
  19. if sys.version_info < (3,0):
  20. filecontents = data.encode("base64")
  21. else:
  22. filecontents = base64.b64encode(data)
  23. filecontents = filecontents.decode(encoding="utf-8", errors="strict")
  24. #Can get file size to check
  25. filesize = os.path.getsize(fullfilename)
  26. file_properties = assetic.Assetic3IntegrationRepresentationsFilePropertiesRepresentation()
  27. file_properties.name = filename
  28. file_properties.mimetype = fileext
  29. file_properties.filecontent = filecontents
  30. file_properties.file_size = filesize
  31. filearray = [file_properties]
  32. #Doc categorisation
  33. document = assetic.Assetic3IntegrationRepresentationsDocumentRepresentation()
  34. document.document_group = 1
  35. document.document_category = 9
  36. document.document_sub_category = 7
  37. document.file_property = filearray
  38. document.parent_type = 'ComplexAsset' #conditional, use if assigning parentID
  39. document.parent_identifier = "AP01"
  40. document.is_key_photo = False
  41. # Perform upload
  42. try:
  43. doc = docapi.document_post(document)
  44. except assetic.rest.ApiException as ex:
  45. asseticsdk.logger.error('Status {0}, Reason: {1} {2}'.format(
  46. e.status, e.reason, e.body))
  47. else:
  48. for docrow in doc:
  49. docid = docrow.get('Id')
  50. print(docid)